﻿namespace Socket.Core
{
    using System;
    using System.Runtime.CompilerServices;

    public abstract class AbstractClient : IClient
    {
        private Socket.Core.ClientConfig m_Config;

        public event EventHandler<SessionArgs> Connected;

        public event EventHandler<SessionArgs> DisConnected;

        public event EventHandler<HeartbeatReceivedArgs> HeartbeatReceived;

        public event EventHandler<ReceivedArgs> Received;

        public AbstractClient(IPackageCommand packageCommand)
        {
            this.PackageCommand = packageCommand;
        }

        public abstract void DisConnect();
        protected virtual void OnConnected(SessionArgs args)
        {
            if (this.Connected != null)
            {
                this.Connected(this, args);
            }
        }

        protected virtual void OnDisConnected(SessionArgs args)
        {
            if (this.DisConnected != null)
            {
                this.DisConnected(this, args);
            }
        }

        protected virtual void OnHeartbeatReceived(HeartbeatReceivedArgs args)
        {
            if (this.HeartbeatReceived != null)
            {
                this.HeartbeatReceived(this, args);
            }
        }

        protected virtual void OnReceived(ReceivedArgs args)
        {
            if (this.Received != null)
            {
                this.Received(this, args);
            }
        }

        public abstract void Setup(Socket.Core.ClientConfig config);
        public abstract bool StartConnect();

        public Socket.Core.ClientConfig ClientConfig { get; protected set; }

        public virtual bool IsConnected
        {
            get
            {
                return ((this.ServerSession != null) && this.ServerSession.IsConnected);
            }
        }

        public IPackageCommand PackageCommand { get; private set; }

        public ISession ServerSession { get; internal set; }
    }
}

